<?php

# http://projects:8081/cdapp-2kt-api/api/journal_cxo.php

include_once '_portal.php';

$method = isset($_GET['method']) ? $_GET['method'] : 'get';

if ($method == 'get' && $post) {
  handle_get($post);
} elseif ($method == 'save' && $post) {
  handle_save($post);
} elseif ($method == 'edit' && $post) {
  handle_edit($post);
}

die();

function handle_save($post) {
  create_table();
  if (isset($post->airport) && isset($post->operation) && isset($post->value)) {
    $ip   = get_client_ip();
    $seat = get_client_seat($ip);
    $sql  = $post->value
    ? "INSERT INTO `2kt_journal_cxo` (`airport`,`operation`,`startTime`,`startIp`,`startSeat`) VALUES ('{$post->airport}','{$post->operation}','{$post->startTime}','{$ip}','{$seat}')"
    : "UPDATE `2kt_journal_cxo` SET `endTime` = '{$post->endTime}' ,`endIp` = '{$ip}', `endSeat` = '{$seat}' WHERE `airport`='{$post->airport}' AND `operation`='{$post->operation}' AND `endTime` IS NULL";
    $result = exec_sql($sql);
    output(['result' => $result, 'sql' => $sql]);
  }
}

function handle_edit($post) {
  create_table();
  $sql    = "UPDATE `2kt_journal_cxo` SET `startTime` = '{$post->startTime}' , `endTime` = '{$post->endTime}' WHERE `number`='{$post->number}'";
  $result = exec_sql($sql);
  output(['result' => $result, 'sql' => $sql]);
}

function handle_get($post) {
  create_table();
  if (isset($post->startTime) && isset($post->endTime)) {
    // query
    $sql = "SELECT * FROM `2kt_journal_cxo` WHERE `startTime` <= '{$post->endTime}' AND `endTime` >='{$post->startTime}' ORDER BY `startTime` DESC";

    $result = exec_sql($sql);

    $content = $result ?: [];

    $status = $result ? true : false;

    output(['status' => $status, 'content' => $content, 'sql' => $sql]);
  } else {
    // status
    $conditions = [
      ['airport' => 'zuuu', 'operation' => 'cco'],
      ['airport' => 'zuuu', 'operation' => 'cdo'],
      ['airport' => 'zuuu', 'operation' => 'va'],
      ['airport' => 'zutf', 'operation' => 'cco'],
      ['airport' => 'zutf', 'operation' => 'cdo'],
      ['airport' => 'zutf', 'operation' => 'pms'],
      ['airport' => 'zutf', 'operation' => 'va'],
    ];

    $sqls    = [];
    $results = [];

    foreach ($conditions as $con) {
      $airport   = $con['airport'];
      $operation = $con['operation'];

      $sql1 = "SELECT * FROM `2kt_journal_cxo` WHERE `airport`='$airport' AND `operation`='{$operation}' AND`endTime` IS NULL ORDER BY `startTime` DESC LIMIT 1";
      $sql2 = "SELECT * FROM `2kt_journal_cxo` WHERE `airport`='$airport' AND `operation`='{$operation}' AND`endTime` IS NOT NULL ORDER BY `endTime` DESC LIMIT 1";

      $result_started = exec_sql($sql1);
      $result_ended   = exec_sql($sql2);

      if (! isset($results[$airport])) {
        $results[$airport] = [];
      }

      if (! isset($results[$airport][$operation])) {
        $results[$airport][$operation] = [];
      }

      $results[$airport][$operation]['started'] = $result_started ? $result_started[0] : null;
      $results[$airport][$operation]['ended']   = $result_ended ? $result_ended[0] : null;
      $sqls[]                                   = $sql1;
      $sqls[]                                   = $sql2;
    }
    output(['results' => $results, 'sqls' => $sqls]);
    return $results;
  }
}

function create_table() {
  $sql = "CREATE TABLE IF NOT EXISTS `2kt_journal_cxo` (
    `number` int(11) NOT NULL AUTO_INCREMENT,
    `airport` varchar(16) COLLATE utf8_unicode_ci NOT NULL,
    `operation` varchar(16) COLLATE utf8_unicode_ci NOT NULL,
    `startTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `endTime` datetime DEFAULT NULL,
    `startIp` varchar(16) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
    `startSeat` varchar(16) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
    `endIp` varchar(16) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
    `endSeat` varchar(16) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
    `createTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updateTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`number`)
    ) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";

  return exec_sql($sql);
}
